

#include "MessageCommand.h"


MessageCommand::MessageCommand()
    :message_("")
    ,xml_doc_(NULL)
{
}

MessageCommand::~MessageCommand()
{
    ReleaseDoc();
}

bool MessageCommand::CreateMsgPacket()
{
    ReleaseDoc();
    xml_doc_=new TiXmlDocument();

    TiXmlElement*   element=new TiXmlElement(STREAM);
    xml_doc_->LinkEndChild(element);
    bool bRet=xml_doc_->Error();
    GenerateMessage();
    return !bRet;
}

std::string MessageCommand::GetMessage() const
{
    return message_;
}

bool MessageCommand::LoadMessage(std::string message)
{
    ReleaseDoc();
    xml_doc_=new TiXmlDocument();
    xml_doc_->Parse(message.c_str());
    bool error=xml_doc_->Error();
    if (error)
    {
        message_="";
        ReleaseDoc();
    }
    else
        message_=message;
    
    return !error;
}

void MessageCommand::ReleaseDoc()
{
    if (xml_doc_!=NULL)
    {
        xml_doc_->Clear();
        delete xml_doc_;
        xml_doc_=NULL;
    }
}

void MessageCommand::GenerateMessage()
{
    if (xml_doc_==NULL)
    {
        message_="";
    }
    else
    {
        TiXmlPrinter* printer=new TiXmlPrinter();
        xml_doc_->Accept(printer);
        message_=std::string(printer->CStr());
        delete printer;
    }
}

bool MessageCommand::HasValidPacket()
{
    if (message_.size()==0||xml_doc_==NULL)
    {
        return false;
    }
    if (xml_doc_->FirstChildElement(STREAM))
    {
        return true;
    }
    return false;
}

bool MessageCommand::LoadFile( const std::string filename )
{
    ReleaseDoc();
    xml_doc_=new TiXmlDocument();
    xml_doc_->LoadFile(filename.c_str());
    bool error=xml_doc_->Error();
    if (error)
    {
        message_="";
        ReleaseDoc();
    }
    else
    {
        GenerateMessage();
    }
    return !error;
}

bool MessageCommand::CreateBody( const std::string body )
{
    TiXmlElement*   element=new TiXmlElement(BODY);
    TiXmlText* text=new TiXmlText(body.c_str());
    element->LinkEndChild(text);
    TiXmlElement* message=FindChild(MESSAGE);
    message->LinkEndChild(element);
    GenerateMessage();
    return true; // TODO(lqjunf@gmail.com):Return Value
}

bool MessageCommand::CreateMessage( const std::string form,const std::string to,const std::string type )
{
    TiXmlElement*   element=new TiXmlElement(MESSAGE);
    element->SetAttribute(FORM,form.c_str());
    element->SetAttribute(TO,to.c_str());
    element->SetAttribute(TYPE,type.c_str());

    TiXmlElement* stream=FindChild(STREAM);
    stream->LinkEndChild(element);
    GenerateMessage();
    return true;// TODO(lqjunf@gmail.com):Return Value
}

TiXmlElement* MessageCommand::FindChild( const std::string node_name )
{
    if (xml_doc_==NULL)
    {
        return NULL;
    }
    else
    {
        if (node_name==STREAM)
        {
            return xml_doc_->FirstChildElement();
        }
        else if (node_name==MESSAGE)
        {
            return xml_doc_->FirstChildElement()->FirstChildElement();
        }
        else if (node_name==BODY)
        {
            return xml_doc_->FirstChildElement()->FirstChildElement()->FirstChildElement();
        }
        else
            return NULL;
    }
    
}

bool MessageCommand::SaveFile( const std::string filename )
{
    if (xml_doc_ == NULL)
    {
        return false;
    }
    return xml_doc_->SaveFile(filename.c_str())==true;
}

